home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / SDL / SDL-Win32-1.2.9-Complete.exe / {app} / include / SDL_net.h < prev    next >
Encoding:
C/C++ Source or Header  |  2003-03-09  |  16.2 KB  |  421 lines

  1. /*
  2.     SDL_net:  An example cross-platform network library for use with SDL
  3.     Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
  4.  
  5.     This library is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU Library General Public
  7.     License as published by the Free Software Foundation; either
  8.     version 2 of the License, or (at your option) any later version.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public
  16.     License along with this library; if not, write to the Free
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     Sam Lantinga
  20.     slouken@libsdl.org
  21. */
  22.  
  23. /* $Id: SDL_net.h,v 1.18 2003/02/10 00:33:26 slouken Exp $ */
  24.  
  25. #ifndef _SDLnet_h
  26. #define _SDLnet_h
  27.  
  28. #include "SDL.h"
  29. #include "SDL_endian.h"
  30. #include "begin_code.h"
  31.  
  32.  
  33.  
  34. /* Set up for C function definitions, even when using C++ */
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38.  
  39. /* Initialize/Cleanup the network API
  40.    SDL must be initialized before calls to functions in this library,
  41.    because this library uses utility functions from the SDL library.
  42. */
  43. extern DECLSPEC int  SDLCALL SDLNet_Init(void);
  44. extern DECLSPEC void SDLCALL SDLNet_Quit(void);
  45.  
  46. /***********************************************************************/
  47. /* IPv4 hostname resolution API                                        */
  48. /***********************************************************************/
  49.  
  50. typedef struct {
  51.     Uint32 host;            /* 32-bit IPv4 host address */
  52.     Uint16 port;            /* 16-bit protocol port */
  53. } IPaddress;
  54.  
  55. /* Resolve a host name and port to an IP address in network form.
  56.    If the function succeeds, it will return 0.
  57.    If the host couldn't be resolved, the host portion of the returned
  58.    address will be INADDR_NONE, and the function will return -1.
  59.    If 'host' is NULL, the resolved host will be set to INADDR_ANY.
  60.  */
  61. #ifndef INADDR_ANY
  62. #define INADDR_ANY        0x00000000
  63. #endif
  64. #ifndef INADDR_NONE
  65. #define INADDR_NONE        0xFFFFFFFF
  66. #endif
  67. #ifndef INADDR_BROADCAST
  68. #define INADDR_BROADCAST    0xFFFFFFFF
  69. #endif
  70. extern DECLSPEC int SDLCALL SDLNet_ResolveHost(IPaddress *address, const char *host, Uint16 port);
  71.  
  72. /* Resolve an ip address to a host name in canonical form.
  73.    If the ip couldn't be resolved, this function returns NULL,
  74.    otherwise a pointer to a static buffer containing the hostname
  75.    is returned.  Note that this function is not thread-safe.
  76. */
  77. extern DECLSPEC const char * SDLCALL SDLNet_ResolveIP(IPaddress *ip);
  78.  
  79.  
  80. /***********************************************************************/
  81. /* TCP network API                                                     */
  82. /***********************************************************************/
  83.  
  84. typedef struct _TCPsocket *TCPsocket;
  85.  
  86. /* Open a TCP network socket
  87.    If ip.host is INADDR_NONE or INADDR_ANY, this creates a local server
  88.    socket on the given port, otherwise a TCP connection to the remote
  89.    host and port is attempted. The address passed in should already be
  90.    swapped to network byte order (addresses returned from 
  91.    SDLNet_ResolveHost() are already in the correct form).
  92.    The newly created socket is returned, or NULL if there was an error.
  93. */
  94. extern DECLSPEC TCPsocket SDLCALL SDLNet_TCP_Open(IPaddress *ip);
  95.  
  96. /* Accept an incoming connection on the given server socket.
  97.    The newly created socket is returned, or NULL if there was an error.
  98. */
  99. extern DECLSPEC TCPsocket SDLCALL SDLNet_TCP_Accept(TCPsocket server);
  100.  
  101. /* Get the IP address of the remote system associated with the socket.
  102.    If the socket is a server socket, this function returns NULL.
  103. */
  104. extern DECLSPEC IPaddress * SDLCALL SDLNet_TCP_GetPeerAddress(TCPsocket sock);
  105.  
  106. /* Send 'len' bytes of 'data' over the non-server socket 'sock'
  107.    This function returns the actual amount of data sent.  If the return value
  108.    is less than the amount of data sent, then either the remote connection was
  109.    closed, or an unknown socket error occurred.
  110. */
  111. extern DECLSPEC int SDLCALL SDLNet_TCP_Send(TCPsocket sock, void *data, int len);
  112.  
  113. /* Receive up to 'maxlen' bytes of data over the non-server socket 'sock',
  114.    and store them in the buffer pointed to by 'data'.
  115.    This function returns the actual amount of data received.  If the return
  116.    value is less than or equal to zero, then either the remote connection was
  117.    closed, or an unknown socket error occurred.
  118. */
  119. extern DECLSPEC int SDLCALL SDLNet_TCP_Recv(TCPsocket sock, void *data, int maxlen);
  120.  
  121. /* Close a TCP network socket */
  122. extern DECLSPEC void SDLCALL SDLNet_TCP_Close(TCPsocket sock);
  123.  
  124.  
  125. /***********************************************************************/
  126. /* UDP network API                                                     */
  127. /***********************************************************************/
  128.  
  129. /* The maximum channels on a a UDP socket */
  130. #define SDLNET_MAX_UDPCHANNELS    32
  131. /* The maximum addresses bound to a single UDP socket channel */
  132. #define SDLNET_MAX_UDPADDRESSES    4
  133.  
  134. typedef struct _UDPsocket *UDPsocket;
  135. typedef struct {
  136.     int channel;        /* The src/dst channel of the packet */
  137.     Uint8 *data;        /* The packet data */
  138.     int len;        /* The length of the packet data */
  139.     int maxlen;        /* The size of the data buffer */
  140.     int status;        /* packet status after sending */
  141.     IPaddress address;        /* The source/dest address of an incoming/outgoing packet */
  142. } UDPpacket;
  143.  
  144. /* Allocate/resize/free a single UDP packet 'size' bytes long.
  145.    The new packet is returned, or NULL if the function ran out of memory.
  146.  */
  147. extern DECLSPEC UDPpacket * SDLCALL SDLNet_AllocPacket(int size);
  148. extern DECLSPEC int SDLCALL SDLNet_ResizePacket(UDPpacket *packet, int newsize);
  149. extern DECLSPEC void SDLCALL SDLNet_FreePacket(UDPpacket *packet);
  150.  
  151. /* Allocate/Free a UDP packet vector (array of packets) of 'howmany' packets,
  152.    each 'size' bytes long.
  153.    A pointer to the first packet in the array is returned, or NULL if the
  154.    function ran out of memory.
  155.  */
  156. extern DECLSPEC UDPpacket ** SDLCALL SDLNet_AllocPacketV(int howmany, int size);
  157. extern DECLSPEC void SDLCALL SDLNet_FreePacketV(UDPpacket **packetV);
  158.  
  159.  
  160. /* Open a UDP network socket
  161.    If 'port' is non-zero, the UDP socket is bound to a local port.
  162.    The 'port' should be given in native byte order, but is used
  163.    internally in network (big endian) byte order, in addresses, etc.
  164.    This allows other systems to send to this socket via a known port.
  165. */
  166. extern DECLSPEC UDPsocket SDLCALL SDLNet_UDP_Open(Uint16 port);
  167.  
  168. /* Bind the address 'address' to the requested channel on the UDP socket.
  169.    If the channel is -1, then the first unbound channel will be bound with
  170.    the given address as it's primary address.
  171.    If the channel is already bound, this new address will be added to the
  172.    list of valid source addresses for packets arriving on the channel.
  173.    If the channel is not already bound, then the address becomes the primary
  174.    address, to which all outbound packets on the channel are sent.
  175.    This function returns the channel which was bound, or -1 on error.
  176. */
  177. extern DECLSPEC int SDLCALL SDLNet_UDP_Bind(UDPsocket sock, int channel, IPaddress *address);
  178.  
  179. /* Unbind all addresses from the given channel */
  180. extern DECLSPEC void SDLCALL SDLNet_UDP_Unbind(UDPsocket sock, int channel);
  181.  
  182. /* Get the primary IP address of the remote system associated with the 
  183.    socket and channel.  If the channel is -1, then the primary IP port
  184.    of the UDP socket is returned -- this is only meaningful for sockets
  185.    opened with a specific port.
  186.    If the channel is not bound and not -1, this function returns NULL.
  187.  */
  188. extern DECLSPEC IPaddress * SDLCALL SDLNet_UDP_GetPeerAddress(UDPsocket sock, int channel);
  189.  
  190. /* Send a vector of packets to the the channels specified within the packet.
  191.    If the channel specified in the packet is -1, the packet will be sent to
  192.    the address in the 'src' member of the packet.
  193.    Each packet will be updated with the status of the packet after it has 
  194.    been sent, -1 if the packet send failed.
  195.    This function returns the number of packets sent.
  196. */
  197. extern DECLSPEC int SDLCALL SDLNet_UDP_SendV(UDPsocket sock, UDPpacket **packets, int npackets);
  198.  
  199. /* Send a single packet to the specified channel.
  200.    If the channel specified in the packet is -1, the packet will be sent to
  201.    the address in the 'src' member of the packet.
  202.    The packet will be updated with the status of the packet after it has
  203.    been sent.
  204.    This function returns 1 if the packet was sent, or 0 on error.
  205.  
  206.    NOTE:
  207.    The maximum size of the packet is limited by the MTU (Maximum Transfer Unit)
  208.    of the transport medium.  It can be as low as 250 bytes for some PPP links,
  209.    and as high as 1500 bytes for ethernet.
  210. */
  211. extern DECLSPEC int SDLCALL SDLNet_UDP_Send(UDPsocket sock, int channel, UDPpacket *packet);
  212.  
  213. /* Receive a vector of pending packets from the UDP socket.
  214.    The returned packets contain the source address and the channel they arrived
  215.    on.  If they did not arrive on a bound channel, the the channel will be set
  216.    to -1.
  217.    The channels are checked in highest to lowest order, so if an address is
  218.    bound to multiple channels, the highest channel with the source address
  219.    bound will be returned.
  220.    This function returns the number of packets read from the network, or -1
  221.    on error.  This function does not block, so can return 0 packets pending.
  222. */
  223. extern DECLSPEC int SDLCALL SDLNet_UDP_RecvV(UDPsocket sock, UDPpacket **packets);
  224.  
  225. /* Receive a single packet from the UDP socket.
  226.    The returned packet contains the source address and the channel it arrived
  227.    on.  If it did not arrive on a bound channel, the the channel will be set
  228.    to -1.
  229.    The channels are checked in highest to lowest order, so if an address is
  230.    bound to multiple channels, the highest channel with the source address
  231.    bound will be returned.
  232.    This function returns the number of packets read from the network, or -1
  233.    on error.  This function does not block, so can return 0 packets pending.
  234. */
  235. extern DECLSPEC int SDLCALL SDLNet_UDP_Recv(UDPsocket sock, UDPpacket *packet);
  236.  
  237. /* Close a UDP network socket */
  238. extern DECLSPEC void SDLCALL SDLNet_UDP_Close(UDPsocket sock);
  239.  
  240.  
  241. /***********************************************************************/
  242. /* Hooks for checking sockets for available data                       */
  243. /***********************************************************************/
  244.  
  245. typedef struct _SDLNet_SocketSet *SDLNet_SocketSet;
  246.  
  247. /* Any network socket can be safely cast to this socket type */
  248. typedef struct {
  249.     int ready;
  250. } *SDLNet_GenericSocket;
  251.  
  252. /* Allocate a socket set for use with SDLNet_CheckSockets()
  253.    This returns a socket set for up to 'maxsockets' sockets, or NULL if
  254.    the function ran out of memory.
  255.  */
  256. extern DECLSPEC SDLNet_SocketSet SDLCALL SDLNet_AllocSocketSet(int maxsockets);
  257.  
  258. /* Add a socket to a set of sockets to be checked for available data */
  259. #define SDLNet_TCP_AddSocket(set, sock) \
  260.             SDLNet_AddSocket(set, (SDLNet_GenericSocket)sock)
  261. #define SDLNet_UDP_AddSocket(set, sock) \
  262.             SDLNet_AddSocket(set, (SDLNet_GenericSocket)sock)
  263. extern DECLSPEC int SDLCALL SDLNet_AddSocket(SDLNet_SocketSet set, SDLNet_GenericSocket sock);
  264.  
  265. /* Remove a socket from a set of sockets to be checked for available data */
  266. #define SDLNet_TCP_DelSocket(set, sock) \
  267.             SDLNet_DelSocket(set, (SDLNet_GenericSocket)sock)
  268. #define SDLNet_UDP_DelSocket(set, sock) \
  269.             SDLNet_DelSocket(set, (SDLNet_GenericSocket)sock)
  270. extern DECLSPEC int SDLCALL SDLNet_DelSocket(SDLNet_SocketSet set, SDLNet_GenericSocket sock);
  271.  
  272. /* This function checks to see if data is available for reading on the
  273.    given set of sockets.  If 'timeout' is 0, it performs a quick poll,
  274.    otherwise the function returns when either data is available for
  275.    reading, or the timeout in milliseconds has elapsed, which ever occurs
  276.    first.  This function returns the number of sockets ready for reading, 
  277.    or -1 if there was an error with the select() system call.
  278. */
  279. extern DECLSPEC int SDLCALL SDLNet_CheckSockets(SDLNet_SocketSet set, Uint32 timeout);
  280.  
  281. /* After calling SDLNet_CheckSockets(), you can use this function on a
  282.    socket that was in the socket set, to find out if data is available
  283.    for reading.
  284. */
  285. #define SDLNet_SocketReady(sock) \
  286.         ((sock != NULL) && ((SDLNet_GenericSocket)sock)->ready)
  287.  
  288. /* Free a set of sockets allocated by SDL_NetAllocSocketSet() */
  289. extern DECLSPEC void SDLCALL SDLNet_FreeSocketSet(SDLNet_SocketSet set);
  290.  
  291.  
  292. /***********************************************************************/
  293. /* Platform-independent data conversion functions                      */
  294. /***********************************************************************/
  295.  
  296. /* Write a 16/32 bit value to network packet buffer */
  297. extern DECLSPEC void SDLCALL SDLNet_Write16(Uint16 value, void *area);
  298. extern DECLSPEC void SDLCALL SDLNet_Write32(Uint32 value, void *area);
  299.  
  300. /* Read a 16/32 bit value from network packet buffer */
  301. extern DECLSPEC Uint16 SDLCALL SDLNet_Read16(void *area);
  302. extern DECLSPEC Uint32 SDLCALL SDLNet_Read32(void *area);
  303.  
  304. /***********************************************************************/
  305. /* Error reporting functions                                           */
  306. /***********************************************************************/
  307.  
  308. /* We'll use SDL's functions for error reporting */
  309. #define SDLNet_SetError    SDL_SetError
  310. #define SDLNet_GetError    SDL_GetError
  311.  
  312. /* I'm eventually going to try to disentangle SDL_net from SDL, thus making
  313.    SDL_net an independent X-platform networking toolkit.  Not today though....
  314.  
  315. extern no_parse_DECLSPEC void SDLCALL SDLNet_SetError(const char *fmt, ...);
  316. extern no_parse_DECLSPEC char * SDLCALL SDLNet_GetError(void);
  317. */
  318.  
  319.  
  320. /* Inline macro functions to read/write network data */
  321.  
  322. /* Warning, some systems have data access alignment restrictions */
  323. #if defined(sparc) || defined(mips)
  324. #define SDL_DATA_ALIGNED    1
  325. #endif
  326. #ifndef SDL_DATA_ALIGNED
  327. #define SDL_DATA_ALIGNED    0
  328. #endif
  329.  
  330. /* Write a 16 bit value to network packet buffer */
  331. #if !SDL_DATA_ALIGNED
  332. #define SDLNet_Write16(value, areap)    \
  333.     (*(Uint16 *)(areap) = SDL_SwapBE16(value))
  334. #else
  335. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  336. #define SDLNet_Write16(value, areap)    \
  337. do                     \
  338. {                    \
  339.     Uint8 *area = (Uint8 *)(areap);    \
  340.     area[0] = (value >>  8) & 0xFF;    \
  341.     area[1] =  value        & 0xFF;    \
  342. } while ( 0 )
  343. #else
  344. #define SDLNet_Write16(value, areap)    \
  345. do                     \
  346. {                    \
  347.     Uint8 *area = (Uint8 *)(areap);    \
  348.     area[1] = (value >>  8) & 0xFF;    \
  349.     area[0] =  value        & 0xFF;    \
  350. } while ( 0 )
  351. #endif
  352. #endif /* !SDL_DATA_ALIGNED */
  353.  
  354. /* Write a 32 bit value to network packet buffer */
  355. #if !SDL_DATA_ALIGNED
  356. #define SDLNet_Write32(value, areap)     \
  357.     *(Uint32 *)(areap) = SDL_SwapBE32(value);
  358. #else
  359. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  360. #define SDLNet_Write32(value, areap)     \
  361. do                    \
  362. {                    \
  363.     Uint8 *area = (Uint8 *)(areap);    \
  364.     area[0] = (value >> 24) & 0xFF;    \
  365.     area[1] = (value >> 16) & 0xFF;    \
  366.     area[2] = (value >>  8) & 0xFF;    \
  367.     area[3] =  value       & 0xFF;    \
  368. } while ( 0 )
  369. #else
  370. #define SDLNet_Write32(value, areap)     \
  371. do                    \
  372. {                    \
  373.     Uint8 *area = (Uint8 *)(areap);    \
  374.     area[3] = (value >> 24) & 0xFF;    \
  375.     area[2] = (value >> 16) & 0xFF;    \
  376.     area[1] = (value >>  8) & 0xFF;    \
  377.     area[0] =  value       & 0xFF;    \
  378. } while ( 0 )
  379. #endif
  380. #endif /* !SDL_DATA_ALIGNED */
  381.  
  382. /* Read a 16 bit value from network packet buffer */
  383. #if !SDL_DATA_ALIGNED
  384. #define SDLNet_Read16(areap)         \
  385.     (SDL_SwapBE16(*(Uint16 *)(areap)))
  386. #else
  387. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  388. #define SDLNet_Read16(areap)         \
  389.     ((((Uint8 *)areap)[0] <<  8) | ((Uint8 *)areap)[1] <<  0)
  390. #else
  391. #define SDLNet_Read16(areap)         \
  392.     ((((Uint8 *)areap)[1] <<  8) | ((Uint8 *)areap)[0] <<  0)
  393. #endif
  394. #endif /* !SDL_DATA_ALIGNED */
  395.  
  396. /* Read a 32 bit value from network packet buffer */
  397. #if !SDL_DATA_ALIGNED
  398. #define SDLNet_Read32(areap)         \
  399.     (SDL_SwapBE32(*(Uint32 *)(areap)))
  400. #else
  401. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  402. #define SDLNet_Read32(areap)         \
  403.     ((((Uint8 *)areap)[0] << 24) | (((Uint8 *)areap)[1] << 16) | \
  404.      (((Uint8 *)areap)[2] <<  8) |  ((Uint8 *)areap)[3] <<  0)
  405. #else
  406. #define SDLNet_Read32(areap)         \
  407.     ((((Uint8 *)areap)[3] << 24) | (((Uint8 *)areap)[2] << 16) | \
  408.      (((Uint8 *)areap)[1] <<  8) |  ((Uint8 *)areap)[0] <<  0)
  409. #endif
  410. #endif /* !SDL_DATA_ALIGNED */
  411.  
  412. #ifdef MACOS_OPENTRANSPORT
  413. #endif
  414. /* Ends C function definitions when using C++ */
  415. #ifdef __cplusplus
  416. };
  417. #endif
  418. #include "close_code.h"
  419.  
  420. #endif /* _SDLnet_h */
  421.